package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.FindSubAccountBean;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.CheckUtils;
import com.hd.crdao.utils.ChineseUtils;
import com.hd.crdao.utils.MobileDesUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/28 0028
 */
public class BindChildAccountActivity extends BaseSecondActivity {
    @BindView(R.id.bindchildaccount_head)
    ExpandHeadView mBindchildaccountHead;
    @BindView(R.id.employeeName)
    EditText mEmployeeName;
    @BindView(R.id.employeePhonenumber)
    EditText mEmployeePhonenumber;
    @BindView(R.id.initPassword)
    EditText mInitPassword;
    @BindView(R.id.doBind)
    Button mDoBind;
    @BindView(R.id.tips)
    TextView mTips;
    private int mTotal;

    @Override
    protected void initView() {

        initData();
        initEvent();
    }


    private void initEvent() {
        mDoBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = mEmployeeName.getText().toString().trim();
                String mobile = mEmployeePhonenumber.getText().toString().trim();
                String loginPassWord = mInitPassword.getText().toString().trim();
                if (TextUtils.isEmpty(userName)) {
                    MyToast.show("请填写员工姓名");
                    return;
                }
                if (!ChineseUtils.checkNameChese(userName)) {
                    MyToast.show("姓名最多为6个汉字");
                    return;
                }
                if (TextUtils.isEmpty(mobile)) {
                    MyToast.show("请填写手机号码");
                    return;
                } else if (mobile.length() != 11 | !CheckUtils.isMobileNO(mobile)) {
                    MyToast.show("输入的手机号不正确");
                    return;
                }
                if (TextUtils.isEmpty(loginPassWord)) {
                    MyToast.show("请填写初始密码");
                    return;
                }
                if (mTotal > 5) {
                    MyToast.show("您已经有5个子账户,最多只能添加5个子账户");
                    return;
                }
                addChildAccount(userName, mobile, loginPassWord);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        checkChildAccountNum();

    }

    /**
     * 检查子账户个数
     */
    private int checkChildAccountNum() {
        OkHttpUtils.post().url(Constants.FINDSUBACCOUNTLIST_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("carBusinessId", SPUtils.getString("businessid", ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                //MyToast.show("服务器异常");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                mTotal = new Gson().fromJson(response, FindSubAccountBean.class).getTotal();
                /*String text = "您当前已有"+mTotal+"个子账户,最多只能绑定5个子账户,账户权限仅支持车辆盘点功能";
                SpannableString spannableString = new SpannableString(text);
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
                spannableString.setSpan(colorSpan, 5, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);*/
                String text = "一个账号最多只能绑定5个子账户,账户权限仅支持车辆盘点功能.";
                mTips.setText(text);
            }
        });
        return mTotal;
    }


    private void addChildAccount(String userName, String mobile, String loginPassWord) {
        HashMap<String, String> map = new HashMap<>();
        map.put("carBusinessId", SPUtils.getString(Config.businessId, ""));
        map.put("userName", userName);
        map.put("mobile", mobile);
        map.put("loginPassWord", MobileDesUtils.strEnc(loginPassWord, Constants.FIRSTKEY, null, null));
        map.put("sign", Constants.SIGN);
        OkHttpUtils.post().url(Constants.ADDCARBUSINESSACCOUNT_URL).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    MyToast.show(msgBean.getMessage());
                    //跳转到我的子账户页面
                    Intent intent = new Intent(UIUtils.getContext(), MyChildAccountActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    MyToast.show(msgBean.getMessage());
                }
            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_bindchildaccount;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mBindchildaccountHead;
    }


}
